<template>
  <div>
    <el-form
      :model="login_form"
      :rules="login_rules"
      ref="login_ref"
      class="w-400px m-auto"
    >
      <el-form-item prop="name" label="">
        <el-input v-model="login_form.name" placeholder="用户名/手机号"></el-input>
      </el-form-item>
      <el-form-item prop="password" label="">
        <el-input
          v-model="login_form.password"
          type="password"
          show-password
          placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="" label=""  class="mt-40px">
        <div class="login cursor-pointer"  @click="handle_login">登录</div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, defineEmits } from "vue";
import { login, user } from "@/api/index";
import { ElMessage } from "element-plus";
import { login_rules } from "../data.config";
import useUserStore from "@/store/modules/user";

let userStore = useUserStore();

const $emit = defineEmits()
const login_ref = ref(null)
const login_form = reactive({
  name: "test",
  password: "jdty@1234",
});

const handle_login = async () => {
  if (!login_ref.value) return;
  await login_ref.value.validate(async (valid, fields) => {
    if (valid) {
      const { data, code } = await login(login_form);
      // console.log("data", data);
      if (code === 200) {
        sessionStorage.setItem('is_front_login', '1')
        await userStore.get_user_info()
        ElMessage.success("登录成功");
        login_form.name = ""
        login_form.password = ""
        
        $emit('close_login', false)
      } else {
        ElMessage.error("登录失败！");
      }
    } else {
    }
  });
};
</script>

<style scoped>
.login {
  width: 514px;
	height: 34px;
	background-color: #bb0615;
	border-radius: 22px;
  font-family: MicrosoftYaHei;
	font-size: 18px;
	line-height: 34px;
	letter-spacing: 18px;
	color: #faf5f5;
  text-align: center;
}
</style>
